﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MCB.Reflection.Extensions
{
    /// <summary>
    /// General extensions for type
    /// </summary>
    public static class TypeGeneralExtensions
    {
        /// <summary>
        /// Whether the given type is a system type
        /// </summary>
        public static bool IsSystemType(this Type type)
        {
            return type.Namespace.StartsWith("System");
        }

        /// <summary>
        /// Determines whether a type is either equal or derived from a
        /// give type
        /// </summary>
        public static bool Is<T>(this Type type)
        {
            Type isType = typeof(T);

            if (type == isType)
                return true;

            if (type.IsSubclassOf(isType))
                return true;

            if (type.GetInterfaces().Where(t => t == isType).Count() > 0)
                return true;

            return false;
        }
    }
}
